package com.ccl.iot.privates;

import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;

import com.ccl.iot.IOTNetAPIEnum;
import com.ccl.iot.App;
import com.ccl.iot.publics.UIUtils;

public class SwitchIconBuilder {
	/*public synchronized static Bitmap Build(int aWidth, int aHeight, int aHighLightColor, int aBackColor, BitmapMask aMask){
		if (aWidth > 0 && aHeight > 0){
			Bitmap tIcon;
			String tKey = "SwitchIcon:" + aWidth + "x" + aHeight + ":" + aHighLightColor + "-" + aBackColor + (aMask == null ? "" : "+" + aMask.getHashCode());
			
			if ((tIcon = BitmapCollector.Get(tKey)) == null){
				tIcon = Bitmap.createBitmap(aWidth, aHeight, aBackColor != Color.TRANSPARENT ? Config.RGB_565 : Config.ARGB_8888);
				
				Canvas tCanvas = new Canvas(tIcon);
				Paint tPaint = new Paint();
				float tRefLen = 0, tCenterX = 0, tCenterY = 0, tXOffset = 0, tYOffset = 0;
				
				if (aWidth > aHeight){
					tCenterY = tRefLen = aHeight * 0.5f;
					tXOffset = (aWidth - aHeight) * 0.5f;
					tCenterX = tCenterY + tXOffset;
				}else{
					tCenterX = tRefLen = aWidth * 0.5f;
					tYOffset = ((aHeight - aWidth) * 0.5f);
					tCenterY = tCenterX + tYOffset;
				}
				
				if (aBackColor != Color.TRANSPARENT){
					tCanvas.drawColor(aBackColor);
				}
				
				tPaint.setAntiAlias(true);
				tPaint.setFilterBitmap(true);
				
				tPaint.setColor(aHighLightColor);
				tPaint.setStyle(Paint.Style.FILL);
				tCanvas.drawCircle(tCenterX, tCenterY, tRefLen, tPaint);
				
				RadialGradient tGradient = new RadialGradient(tCenterX, tCenterY, tRefLen * 0.95f, new int[]{Color.WHITE, Color.WHITE, Color.WHITE, Color.WHITE, Color.LTGRAY, Color.GRAY}, null, Shader.TileMode.CLAMP);
				tPaint.setShader(tGradient);
				
				tCanvas.drawCircle(tCenterX, tCenterY, tRefLen * 0.95f, tPaint);
	
				Path tPath = new Path();
				tPath.moveTo(tRefLen * 0.845f + tXOffset, tRefLen * 0.453f + tYOffset);
				tPath.arcTo(new RectF(tRefLen * 0.405f + tXOffset, tRefLen * 0.433f + tYOffset, tRefLen * 1.594f + tXOffset, tRefLen * 1.622f + tYOffset), -105, -330);
				tPath.lineTo(tRefLen * 1.155f + tXOffset, tRefLen * 0.615f + tYOffset);
				tPath.arcTo(new RectF(tRefLen * 0.559f + tXOffset, tRefLen * 0.586f + tYOffset, tRefLen * 1.441f + tXOffset, tRefLen * 1.468f + tYOffset), -69, 319);
				tPath.close();
				
				tPaint.setShader(null);
				tPaint.setStyle(Paint.Style.FILL_AND_STROKE);
				tPaint.setColor(aHighLightColor);
				tCanvas.drawPath(tPath, tPaint);
				
				tCanvas.drawRect(new RectF(tRefLen * 0.924f + tXOffset, tRefLen * 0.332f + tYOffset, tRefLen * 1.076f + tXOffset, tRefLen * 0.705f + tYOffset), tPaint);
				
				tCanvas.drawCircle(tCenterX, tRefLen * 0.332f + tYOffset, tRefLen * 0.076f, tPaint);
				tCanvas.drawCircle(tCenterX, tRefLen * 0.705f + tYOffset, tRefLen * 0.076f, tPaint);
				
				if (aMask != null){
					aMask.paintMask(tCanvas);
				}
				
				tCanvas.save();
				tCanvas.restore();
				
				BitmapCollector.Add(tKey, tIcon);
				
				return tIcon;
			}else{
				return tIcon;
			}
		}
		
		return null;
	}*/

	// =============================modify start====================================
	public synchronized static Bitmap Build(int aWidth, int aHeight, int aHighLightColor, int aBackColor, boolean isOn, boolean isPressed, BitmapMask aMask, int iconId) {
		if (aWidth > 0 && aHeight > 0) {
			Bitmap tIcon;
			String tKey = "SwitchIcon:" + aWidth + "x" + aHeight + ":" + aHighLightColor + "-" + aBackColor + (aMask == null ? "" : "+" + aMask.getHashCode()) + "." + iconId;

			// System.out.println("SwitchIconBuilder---> DeviceIconProvider.ICON_ID: "+iconId);

			if ((tIcon = BitmapCollector.Get(tKey)) == null) {
				tIcon = Bitmap.createBitmap(aWidth, aHeight, aBackColor != Color.TRANSPARENT ? Config.RGB_565 : Config.ARGB_4444);

				Canvas tCanvas = new Canvas(tIcon);

				Bitmap centerIcon = null;

				// TODO 设置设备页面灯的图标和批处理图标
//
					if(isOn){
						centerIcon = BitmapFactory.decodeResource(App.GetInstance().getResources(), iconId);
					}else{
						centerIcon = BitmapFactory.decodeResource(App.GetInstance().getResources(), iconId);
					}
//
				Paint paint = new Paint();
				/*if (isOn) {
					centerIcon = BitmapFactory.decodeResource(App.GetInstance().getResources(), DeviceIconProvider.ICON_LIST_SWITCH_ON[iconId]);
				} else {
					centerIcon = BitmapFactory.decodeResource(App.GetInstance().getResources(), DeviceIconProvider.ICON_LIST_SWITCH_OFF[iconId]);
				}
				
				if (isPressed) {
					for (int i = 0; i < centerIcon.getWidth(); i++) {
						for (int j = 0; j < centerIcon.getHeight(); j++) {
							// int color = Color.FromArgb(CompelColor & 0x0000ff, (CompelColor & 0x00ff00) >> 8, (CompelColor & 0xff0000) >> 16);
							Bitmap.createBitmap(centerIcon).setPixel(i, j, Color.GRAY);
						}
					}
					paint.setAlpha(180);
				}*/

				int size = aWidth > aHeight ? aHeight : aWidth;

				centerIcon = UIUtils.scaleBitmap(centerIcon, size, size);

				float left = (aWidth - centerIcon.getWidth()) / 2;
				float top = (aHeight - centerIcon.getHeight()) / 2;

				if (aBackColor != Color.TRANSPARENT) {
					tCanvas.drawColor(aBackColor);
				}

				if(isPressed){
					paint.setAlpha(70);
				}

				tCanvas.drawBitmap(centerIcon, left, top, paint);

				if (aMask != null) {
					aMask.paintMask(tCanvas);
				}

				tCanvas.save();
				tCanvas.restore();

				BitmapCollector.Add(tKey, tIcon);

				return tIcon;
			} else {
				return tIcon;
			}
		}

		return null;
	}

	// =============================modify end====================================

	public static Bitmap Build(int aWidth, int aHeight, boolean aOnline, boolean aIsOn, boolean aPressing, int aAuth, int iconId) {
		Bitmap tIcon = null;
		int tColor = Color.DKGRAY;

		if (aOnline) {
			BitmapMask tMask = null;

			if (aAuth == IOTNetAPIEnum.EAUTH_STATE.EAUTH_STATE_UNKNOWN || aAuth == IOTNetAPIEnum.EAUTH_STATE.EAUTH_STATE_NONE || aAuth == IOTNetAPIEnum.EAUTH_STATE.EAUTH_STATE_NONE
					|| aAuth == IOTNetAPIEnum.EAUTH_STATE.EAUTH_STATE_WAITTING || aAuth == IOTNetAPIEnum.EAUTH_STATE.EAUTH_STATE_REJECT || aAuth == IOTNetAPIEnum.EAUTH_STATE.EAUTH_STATE_QUIT) {
				aIsOn = false;
				aPressing = false;
				tMask = new ForbidMask(new RectF(aWidth * 0.7f, aHeight * 0.7f, aWidth, aHeight), Color.WHITE);
			} else if (aAuth == IOTNetAPIEnum.EAUTH_STATE.EAUTH_STATE_READ) {
				aPressing = false;
				tMask = new WarningMask(new RectF(aWidth * 0.7f, aHeight * 0.7f, aWidth, aHeight), Color.WHITE);
			}

			if (aIsOn) {
				if (aPressing) {
					tColor = Color.rgb(100, 200, 100);
				} else {
					tColor = Color.GREEN;
				}
			} else {
				if (aPressing) {
					tColor = Color.rgb(200, 100, 100);
				} else {
					tColor = Color.RED;
				}
			}

			/*tIcon = Build(aWidth, aHeight, tColor, Color.TRANSPARENT, tMask);				
			}else{
			tIcon = Build(aWidth, aHeight, tColor, Color.TRANSPARENT, null);
			}
			*/

			// ==============================modify start===========================================

			tIcon = Build(aWidth, aHeight, tColor, Color.TRANSPARENT, aIsOn, aPressing, tMask, iconId);
		} else {
			tIcon = Build(aWidth, aHeight, tColor, Color.TRANSPARENT, aIsOn, aPressing, null, iconId);
			for (int i = 0; i < tIcon.getWidth(); i+=3) {
				for (int j = 0; j < tIcon.getHeight(); j+=3) {
					int pixel = tIcon.getPixel(i, j);
					
					tIcon.setPixel(i, j, pixel & 0x32323232);
				}
			}
		}
		// ==============================modify end===========================================

		return tIcon;
	}

}
